package com.pi.shisan.service.config;

import com.pi.shisan.service.schedule.ScheduleCreater;
import com.pi.shisan.service.schedule.job.SecondNotifyJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StartConfig implements ApplicationRunner {

    @Autowired
    private TableConfig tableConfig;

    @Autowired
    private SecondNotifyJob secondNotifyJob;

    @Autowired
    private ScheduleCreater scheduleCreater;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info(">>>>>>> 钟表系统已启动，开始最后阶段的准备工作 <<<<<<<<");
        new Thread(()->{
            try {
                log.info("开始检查表结构");
                tableConfig.init();
                log.info("表结构检查完成");
            } catch (Exception e) {
                log.error("表结构初始化失败：",e);
            }
            log.info("开始检查并打开屏显");
            secondNotifyJob.checkAndStartShow();
            log.info("检查并打开屏显完成");
            try {
                log.info("开始启动时钟定时器");
                scheduleCreater.getScheduler().start();
                log.info("时钟定时器启动完成");
            } catch (SchedulerException e) {
                log.error("时钟定时器启动失败：",e);
            }
            log.info(">>>>>>> 钟表启动完成 <<<<<<<<");
        }).start();
    }
}
